const fs = require('fs');
const path = require( 'path' );

let pathname = path.resolve( __dirname , 'hello.txt' );

let data = '上海\n自来水\n来自\n海上';

let options = { encoding: 'utf8' }
// let options = 'utf8';

let callback = error => {
    if( error ) {
        console.log( '文件写出错误: ' , error );
        return ;
    }
    console.log( '文件写出成功' );
}

// fs.writeFile( pathname , data [, options] , callback)
let x = fs.writeFile( pathname , data , options , callback );
console.log( 'x: ' , x );

// 自行测试 writeFileSync 函数
// https://nodejs.org/dist/latest-v16.x/docs/api/fs.html#fswritefilesyncfile-data-options
// let x = fs.writeFileSync( pathname , data [, options] )